package com.fare.taluo.web.action;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.fare.taluo.BaseFdcAction;
import com.fare.taluo.application.plane.channel.AirCompanyService;
import com.fare.taluo.application.plane.city.PortApplication;
import com.fare.taluo.application.plane.flight.FlightInfoApplication;
import com.fare.taluo.application.plane.income.OccupRateApplication;
import com.fare.taluo.util.DateJsonValueProcessor;
import com.fare.taluo.util.StringAndDateAndFileUtil;
import com.fare.taluo.vo.core.FlightInfoVO;
import com.fare.taluo.vo.core.OccupRateEntityVO;
/**
 * 收益数据（保留）
 * @author Administrator
 *
 */
@Namespace("/")
@Controller("occupRateAction")
@Scope("prototype")
public class OccupRateAction extends BaseFdcAction{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 5976658312848927488L;
	private String returnPageURL;
	private String listString;
	
	//保存air的List
	private String air_listStr;
	
	//保存line的List
	private String flight_listStr;
	
	//保存时间
	private String flightDate;
	private OccupRateEntityVO occupRateEntityVO;
	
	//页码
	private int pageNum;// 页码
	private int pageSize;// 每页数量
	
	//查询条件设置
	private String jspData;
	private String flightNo;
	
	@Resource(name="occupRateApplication")
	private OccupRateApplication occupRateApplication;
	
	@Resource(name="airCompanyServiceImpl")
	private AirCompanyService airCompanyService;
	
	@Resource(name="flightInfoApplication")
	private FlightInfoApplication flightInfoApplication;
	
	@Resource(name="portApplication")
	private PortApplication portApplication;
	
	public void initAirCom() throws Exception{
		
	}
	
	public void initFlightInfo() throws IOException{
		List<FlightInfoVO>  flight_list=flightInfoApplication.findByFlightNo(flightNo);
		FlightInfoVO fvo=null;
		for(int i=0;i<flight_list.size();i++){
			fvo=new FlightInfoVO();
			fvo=flight_list.get(i);
			fvo.setFlightName(fvo.getFromCityName()+"—"+fvo.getToCityName());
		}
		JSONArray ja = JSONArray.fromObject(flight_list);   
		flight_listStr=ja.toString();
		ServletActionContext.getResponse().setContentType("utf-8");
		ServletActionContext.getResponse().getWriter().write(flight_listStr);
	}
	
	
	//保存数据
	public void save(){
		if(occupRateEntityVO!=null){
			try {
				occupRateEntityVO.setFlightDay(StringAndDateAndFileUtil.formatDateString(flightDate, "yyyy-MM-dd HH:mm"));
			//	String lineCode = occupRateEntityVO.getLineCode();
				//页面传输过来的值要转换与一下
				occupRateEntityVO.setFlightInfo(occupRateEntityVO.getFlightInfo().replace(",", "").trim());
				occupRateApplication.saveOrUpdateOccupRateEntity(occupRateEntityVO);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	//删除数据
	public void deleteOccup() throws Exception{
		occupRateApplication.deleteOccupRateEntity(occupRateEntityVO.getId());
	}
	
	//需要显示路径和时间点
	public void show() throws Exception{
        JSONObject jsonObject = JSONObject.fromObject(jspData);         
        Iterator keyIter = jsonObject.keys();         
        String key;         
        Object value;         
        HashMap valueMap = new HashMap();         
        
        while (keyIter.hasNext()) {         
            key = (String) keyIter.next();         
            value = jsonObject.get(key);         
            valueMap.put(key, value);         
        }
        
		String queryComVal=(String) valueMap.get("queryComVal");
		String queryDate=(String) valueMap.get("queryDate");
		String depCity=(String) valueMap.get("depCity");	
		String arrCity=(String) valueMap.get("arrCity");
		String fromCity = "";
		String toCity = "";
		Date queryDa=null;
		if(queryDate!=null && !"".equals(queryDate)){	
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); 
			queryDa=df.parse(queryDate);
		}
		if(pageNum <= 0){
			pageNum = 1;
		}
		if(pageSize == 0 || pageSize == 10){
			pageSize = 10;
		}
		if(!"中文/拼音".equals(depCity)&& !"".equals(depCity)){
			fromCity=portApplication.findPortEntityByCityName(depCity).getPortCode();
		}
		if(!"中文/拼音".equals(arrCity) && !"".equals(arrCity)){
			toCity=portApplication.findPortEntityByCityName(arrCity).getPortCode();
		}
		/*String queryLine="";
		if(!"中文/拼音".equals(depCity)&& !"".equals(depCity)){
			queryLine=depCity;
		}
		if(!"中文/拼音".equals(arrCity) && !"".equals(arrCity)){
			queryLine+="-"+arrCity;
		}
		if("-".equals(queryLine)){
			queryLine=null;
		}*/
		//把首都的编码去掉
		/*if(queryLine!=null || !"".equals(queryLine)){
			if(queryLine.contains("(PEK)")){
				queryLine=queryLine.replace("(PEK)", "");
			}
		}*/
		
		//按照各种条件查询
		//List<OccupRateEntityVO> list=occupRateApplication.findOccupRateByConsdions(queryComVal, queryLine, queryDa, pageNum, pageSize);;
		List<OccupRateEntityVO> list=occupRateApplication.findOccupRateByCode(queryComVal, fromCity, toCity, queryDa, pageNum, pageSize);
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Date.class,new DateJsonValueProcessor("yyyy-MM-dd HH:mm"));
		
		JSONObject tjson = new JSONObject();
		tjson.put("Rows", JSONArray.fromObject(list,jsonConfig).toString());
		tjson.put("Total",occupRateApplication.getTotalOccupRateListByCodition(queryComVal, fromCity,toCity, queryDa));
		
		listString=tjson.toString();

		ServletActionContext.getResponse().setContentType("utf-8");
		ServletActionContext.getResponse().getWriter().write(listString);
	}
	
	public String showPage() throws Exception{
		returnPageURL = "/fare/_syrgdrsj.jsp";
		return "dispatcher";
	}
  

	public String getReturnPageURL() {
		return returnPageURL;
	}

	public void setReturnPageURL(String returnPageURL) {
		this.returnPageURL = returnPageURL;
	}

	public String getListString() {
		return listString;
	}

	public void setListString(String listString) {
		this.listString = listString;
	}
	
	

	public String getFlight_listStr() {
		return flight_listStr;
	}

	public void setFlight_listStr(String flight_listStr) {
		this.flight_listStr = flight_listStr;
	}

	public String getAir_listStr() {
		return air_listStr;
	}

	public void setAir_listStr(String air_listStr) {
		this.air_listStr = air_listStr;
	}

	
	public String getFlightDate() {
		return flightDate;
	}

	public void setFlightDate(String flightDate) {
		this.flightDate = flightDate;
	}

	
	public OccupRateEntityVO getOccupRateEntityVO() {
		return occupRateEntityVO;
	}

	public void setOccupRateEntityVO(OccupRateEntityVO occupRateEntityVO) {
		this.occupRateEntityVO = occupRateEntityVO;
	}



	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public String getJspData() {
		return jspData;
	}

	public void setJspData(String jspData) {
		this.jspData = jspData;
	}

	public String getFlightNo() {
		return flightNo;
	}

	public void setFlightNo(String flightNo) {
		this.flightNo = flightNo;
	}

}
